<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.Connection"%>
<%@page import="jobportal.Dbconn"%>
<%@ page import="java.io.*" %>

<%@ page import="java.util.zip.*"%>
<%
            try {
                String pic = null;
                String username = (String) session.getAttribute("username");
                String table = "", col = "", pk_col = "", uname = "";
                //RequestDispatcher rd;

                if (session.getAttribute("role").equals("student")) {
                    table = "student";
                    pk_col = "username";
                    col = "photo";

                } else if (session.getAttribute("role").equals("college")) {
                    table = "college";
                    pk_col = "clgid";
                    col = "logo";
                } else if (session.getAttribute("role").equals("company")) {
                    table = "company";
                    col = "logo";
                    pk_col = "compid";

                } else if (session.getAttribute("role").equals("admin")) {
                    table = "admin";
                    col = "photo";
                    pk_col = "name";

                } else {
                    session.invalidate();
                    response.sendRedirect("../index.jsp");
                }



                String saveFile = "";
                String contentType = request.getContentType();
                if ((contentType != null) && (contentType.indexOf("multipart/form-data") >= 0)) {
                    DataInputStream in = new DataInputStream(request.getInputStream());
                    int formDataLength = request.getContentLength();
                    byte dataBytes[] = new byte[formDataLength];
                    int byteRead = 0;
                    int totalBytesRead = 0;
                    while (totalBytesRead < formDataLength) {
                        byteRead = in.read(dataBytes, totalBytesRead, formDataLength);
                        totalBytesRead += byteRead;
                    }
                    String file = new String(dataBytes);
                    saveFile = file.substring(file.indexOf("filename=\"") + 10);
                    saveFile = saveFile.substring(0, saveFile.indexOf("\n"));
                    saveFile = saveFile.substring(saveFile.lastIndexOf("\\") + 1, saveFile.indexOf("\""));
                    int lastIndex = contentType.lastIndexOf("=");
                    String boundary = contentType.substring(lastIndex + 1, contentType.length());
                    int pos;
                    pos = file.indexOf("filename=\"");
                    pos = file.indexOf("\n", pos) + 1;
                    pos = file.indexOf("\n", pos) + 1;
                    pos = file.indexOf("\n", pos) + 1;
                    int boundaryLocation = file.indexOf(boundary, pos) - 4;
                    int startPos = ((file.substring(0, pos)).getBytes()).length;
                    int endPos = ((file.substring(0, boundaryLocation)).getBytes()).length;
                    File ff = new File(saveFile);
                    FileOutputStream fileOut = new FileOutputStream(ff);
                    fileOut.write(dataBytes, startPos, (endPos - startPos));
                    fileOut.flush();
                    fileOut.close();
                    //Successfully uploaded file to object

                    out.println(saveFile);
                    FileInputStream fis;
                    File f = new File(saveFile);
                    Connection con = Dbconn.getConn();

                    String sql = "update " + table + " set " + col + "=? where " + pk_col + "=?";
                    PreparedStatement psmnt = con.prepareStatement(sql);
                    fis = new FileInputStream(f);
                    psmnt.setBinaryStream(1, (InputStream) fis, (int) (f.length()));
                    psmnt.setString(2, username);
                    int s = psmnt.executeUpdate();
                    if (s > 0) {
                        request.setAttribute("status", "Image uploaded Successfully");


                    } else {
                        request.setAttribute("status", "Image upload failed");
                    }

                }
                if (session.getAttribute("role").equals("student")) {
                    response.sendRedirect("../student/photo.jsp");
                }

                if (session.getAttribute("role").equals("college")) {
                    response.sendRedirect("../College/collegelogo.jsp");
                }

                if (session.getAttribute("role").equals("company")) {
                    response.sendRedirect("../Company/companylogo.jsp");
                }

                if (session.getAttribute("role").equals("admin")) {
                    response.sendRedirect("../admin/photo.jsp");
                }




            } catch (Exception e) {
                String status = "";
                if (e.toString().contains("com.mysql.jdbc.PacketTooBigException")) {
                   
                    response.sendRedirect("../student/photo.jsp?status=file size too large!max allowed size is 1MB");
                    if (session.getAttribute("role").equals("student")) {
                    }
                    response.sendRedirect("../student/photo.jsp?status=file size too large!max allowed size is 1MB");

                    if (session.getAttribute("role").equals("company")) {
                        response.sendRedirect("../Company/companylogo.jsp?status=file size too large!max allowed size is 1MB");
                    }
                }
                request.setAttribute("status", "Image upload failed");
                if (session.getAttribute("role").equals("student")) {
                }
                response.sendRedirect("../student/photo.jsp?status="+e);

                if (session.getAttribute("role").equals("company")) {
                    response.sendRedirect("../Company/companylogo.jsp?status="+e);
                }
            }

%>
